package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zprmb.udzt.bean.InvestOldJILU_Bean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.TimeUtils;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 投资记录
 * @author shenshao
 *
 */
public class InvestOldJiluActivity extends BaseActivity implements
		OnClickListener {

	private ImageView iv_v4_investjilu_back;
	private TextView tv_v4_investjilu_title;

	private ListView lv_invest_jilu;
	private ImageView iv_invest_null;
	private String investId;
	private String loanTitle;
	private String type;
	private MyAdapter adapter1;
	private ArrayList<InvestOldJILU_Bean> list = new ArrayList<InvestOldJILU_Bean>();
	private TextView tv_v4_investoldjilu_title;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (list.size() < 1) {
				iv_invest_null.setVisibility(View.VISIBLE);
				lv_invest_jilu.setVisibility(View.GONE);
			} else {
				iv_invest_null.setVisibility(View.GONE);
				lv_invest_jilu.setVisibility(View.VISIBLE);
				adapter1 = new MyAdapter();
				lv_invest_jilu.setAdapter(adapter1);
			}
		};
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v4_investoldjilu_activity);
		 BbtApplication.getInstance().addActvity(this);
		investId = getIntent().getStringExtra("id");
		type = getIntent().getStringExtra("type");
		loanTitle = getIntent().getStringExtra("loanTitle");
		initView();
		initData();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initView() {
		iv_v4_investjilu_back = (ImageView) findViewById(R.id.iv_v4_investjilu_back);
		iv_v4_investjilu_back.setOnClickListener(this);

		tv_v4_investjilu_title = (TextView) findViewById(R.id.tv_v4_investjilu_title);
		tv_v4_investjilu_title.getPaint().setFakeBoldText(true);
		tv_v4_investjilu_title.setText("回款计划");

		lv_invest_jilu = (ListView) findViewById(R.id.lv_invest_jilu);
		iv_invest_null = (ImageView) findViewById(R.id.iv_invest_null);

		// tv_v4_investjilu_1 = (TextView)
		// findViewById(R.id.tv_v4_investjilu_1);
		// tv_v4_investjilu_2 = (TextView)
		// findViewById(R.id.tv_v4_investjilu_2);
		// tv_v4_investjilu_3 = (TextView)
		// findViewById(R.id.tv_v4_investjilu_3);
		// tv_v4_investjilu_4 = (TextView)
		// findViewById(R.id.tv_v4_investjilu_4);
		tv_v4_investoldjilu_title = (TextView) findViewById(R.id.tv_v4_investoldjilu_title);
		tv_v4_investoldjilu_title.setText(loanTitle);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_v4_investjilu_back:
			finish();
			break;
		}
	}

	private void initData() {
		AjaxParams params = new AjaxParams();
		params.put("id", investId);
		params.put("type", type);
		VolleryUtil vollery = new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("汇款计划", jsonObject.toString());
				if (jsonObject.optInt("success") == 1) {
					JSONArray json =jsonObject.optJSONArray("data");
					for (int i = 0; i < json.length(); i++) {
						InvestOldJILU_Bean jilu = new InvestOldJILU_Bean();
						JSONObject j;
						try {
							j = (JSONObject) json.get(i);
							jilu.setCurrentPeriod(j.optString("currentPeriod"));
							jilu.setInvestId(j.optString("investId"));
							jilu.setLoanId(j.optString("loanId"));
							jilu.setLoanName(j.optString("loanName"));
							jilu.setShouldPayMoney(j.optString("shouldPayMoney"));
							jilu.setTime(j.optString("time"));
							jilu.setTotalPeriod(j.optString("totalPeriod"));
							jilu.setType(j.optString("type"));
							list.add(jilu);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				handler.sendEmptyMessage(1);
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		vollery.setHeaders("token", getToken());
		vollery.setHeaders("client", "android");
		vollery.setShow(true);
		vollery.setRequestMethod(0);
		vollery.execute(InvestOldJiluActivity.this, Constant.URL
				+ "apiAccount/investRecordsById?", params.toString());
		Log.e("回款网址", Constant.URL+"apiAccount/investRecordsById?"+params.toString());
		
	}

	public class MyAdapter extends BaseAdapter {
		public int getCount() {
			return list.size();
		}

		public Object getItem(int arg0) {
			return arg0;
		}

		public long getItemId(int arg0) {
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup arg2) {
			InvestOldJILU_Bean jilu = list.get(position);
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(InvestOldJiluActivity.this,
						R.layout.v4_item_invest_jilu, null);
				holder.tv_v4_item_investjilu_1 = (TextView) convertView
						.findViewById(R.id.tv_v4_item_investjilu_1);
				holder.tv_v4_item_investjilu_2 = (TextView) convertView
						.findViewById(R.id.tv_v4_item_investjilu_2);
				holder.tv_v4_item_investjilu_3 = (TextView) convertView
						.findViewById(R.id.tv_v4_item_investjilu_3);
				holder.tv_v4_item_investjilu_4 = (TextView) convertView
						.findViewById(R.id.tv_v4_item_investjilu_4);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_v4_item_investjilu_1.setText(jilu.getCurrentPeriod());
			holder.tv_v4_item_investjilu_2.setText(jilu.getType());
			holder.tv_v4_item_investjilu_3.setText(jilu.getShouldPayMoney());
			holder.tv_v4_item_investjilu_4.setText(TimeUtils
					.formatTime(new Date(Long.valueOf(jilu.getTime()))));
			return convertView;
		}

		public class ViewHolder {
			TextView tv_v4_item_investjilu_1;
			TextView tv_v4_item_investjilu_2;
			TextView tv_v4_item_investjilu_3;
			TextView tv_v4_item_investjilu_4;
		}
	}

}
